package com.ruoyi.aop;

import com.ruoyi.activiti.domain.dto.WorkflowTrajectory;
import com.ruoyi.activiti.service.impl.ProcessFlowsService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.MailUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.reim.domain.WorkflowReim;
import com.ruoyi.reim.service.WorkflowReimService;
import com.ruoyi.system.service.ISysUserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>邮件发送处理类</p>
 * <p>创建日期：2021-08-05</p>
 *
 * @author zhou-qf
 */
@Aspect
@Component
public class MailAspect {

    private Logger logger = LoggerFactory.getLogger(MailAspect.class);

    @Autowired
    private WorkflowReimService workflowReimService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ProcessFlowsService processFlowsService;

    /**
     * 定义切入点
     */
    @Pointcut("execution(public * com.ruoyi.activiti.service.impl.WorkflowTrajectoryService.insertWorkflowTrajectory(..))")
    public void insertPoint(){}

    /**
     * 定义切入点
     */
    @Pointcut("execution(public * com.ruoyi.activiti.service.impl.WorkflowTrajectoryService.updateWorkflowTrajectory(..))")
    public void updatePoint(){}

    /**
     * 发送邮件
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    @Around("updatePoint()")
    public Object doUpdateAdvice(ProceedingJoinPoint proceedingJoinPoint){
        try {
            // 获取轨迹信息
            WorkflowTrajectory workflowTrajectory = (WorkflowTrajectory)proceedingJoinPoint.getArgs()[0];
            // 获取基本信息
            WorkflowReim workflowReim = workflowReimService.selectWorkflowReimById(workflowTrajectory.gettId());
            // 判断是否有下个审批节点，如果没有，表示当前是最后一个审批节点
            List<String> stringList = processFlowsService.nextNode(workflowReim.getInstanceId());

            if(stringList.isEmpty()){
                // 获取员工姓名
                SysUser sysUser = sysUserService.selectUserByUserName(workflowReim.getCreateBy());
                // 判断当前审批是通过还是不通过
                String content = "";
                String title = "";
                if("同意".equals(workflowTrajectory .getApprovalResult())){
                    title = "【审批平台】审批通过通知";
                    content = sysUser.getNickName() +",您好：<br>" +  "您提交的单据【" + workflowReim.getBillCode() + "】,审批结束,请您登录网报系统进行封皮打印后提交到报销秘书处。";
                } else {
                    title = "【审批平台】审批驳回通知";
                    content = sysUser.getNickName() +",您好：<br>" +  "您提交的单据【" + workflowReim.getBillCode() + "】," + SecurityUtils.getNickName() + "审批不通过,批注：{" + workflowTrajectory.getApprovalContent() + "},请您登录网报系统进行修改。";
                }
                MailUtils.sendMail(title, content, new String[]{sysUser.getEmail()});
            }
            return proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            logger.error("发送邮件错误：{}", throwable);
        }
        return null;
    }

    /**
     * 发送邮件
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    @Around("insertPoint()")
    public Object doInsertAdvice(ProceedingJoinPoint proceedingJoinPoint){
        try {
            // 获取轨迹信息
            WorkflowTrajectory workflowTrajectory = (WorkflowTrajectory)proceedingJoinPoint.getArgs()[0];
            // 获取基本信息
            WorkflowReim workflowReim = workflowReimService.selectWorkflowReimById(workflowTrajectory.gettId());
            // 获取员工姓名
            SysUser sysUser = sysUserService.selectUserByUserName(workflowReim.getCreateBy());
            // 判断是员工提交还是领导审批
            String content = "";
            if(workflowTrajectory.isFirst()){
                content = "您好：<br>" + sysUser.getNickName() + "员工提交的单据【" + workflowReim.getBillCode() + "】,请您尽快登录审批平台进行审批,地址：<a href='www.baidu.com'>www.baidu.com</a>";
            } else {
                content = "您好：<br>" + sysUser.getNickName() + "员工提交的单据【" + workflowReim.getBillCode() + "】, " + SecurityUtils.getNickName()  + "已审批通过,请您尽快登录审批平台进行审批,地址：<a href='www.baidu.com'>www.baidu.com</a>";
            }

            MailUtils.sendMail("【审批平台】请您尽快审批人员报销信息", content,  workflowTrajectory.getApprovalEmail().split(","));
            return proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            logger.error("发送邮件错误：{}", throwable);
        }
        return null;
    }
}
